/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.service.impl;

import cz.muni.fi.pa165.machineryrental.converters.MachineDTOConverter;
import cz.muni.fi.pa165.machineryrental.dao.MachineDAO;
import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.entities.Machine;
import cz.muni.fi.pa165.machineryrental.service.MachineService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of MachineService
 *
 * @author Marian Lorenc, 374122
 */
@Service("MachineService")

public class MachineServiceImpl implements MachineService{

    @Autowired
    private MachineDAO machineDAO;
    
    @Autowired
    public MachineServiceImpl(MachineDAO machineDAO) {
        if (machineDAO == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }
        
        this.machineDAO = machineDAO;
    }    
    
    @Transactional(readOnly = true)
    public MachineDTO getById(long id) {
       
        Machine machine = machineDAO.getById(id);

        return MachineDTOConverter.toDTO(machine);
    }

    @Transactional
    public void create(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }
        Machine entity = MachineDTOConverter.toEntity(machine);
        machineDAO.create(entity);
    }

    @Transactional
    public void update(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }
        if (machine.getId() <= 0) {
            throw new IllegalArgumentException("MachineDAO unexists");
        }

        Machine entity = machineDAO.getById(machine.getId());        
        entity.setBrand(machine.getBrand());
        entity.setCode(machine.getCode());
        //entity.setId(machine.getId());
        entity.setModel(machine.getModel());
        entity.setYearOfProduction(machine.getYearOfProduction());
        entity.setBrand(machine.getBrand());
         
        machineDAO.update(entity);
    }

    @Transactional
    public void remove(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("Given MachineDAO cannot be null.");
        }
        if (machine.getId() <= 0) {
            throw new IllegalArgumentException("MachineDAO unexists");
        }
        
        Machine entity = machineDAO.getById(machine.getId());        
        machineDAO.remove(entity);
        machine.setId(0);
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findAll() {
        List<Machine> machines = machineDAO.findAll();
        
        List<MachineDTO> result = new ArrayList<MachineDTO>();
        for (Machine r : machines) {
            result.add(MachineDTOConverter.toDTO(r));
        }
        return result;
    }

    @Transactional(readOnly = true)
    public MachineDTO findByCode(String code) {
        if (code == null) {
            throw new IllegalArgumentException("Given code cannot be null.");
        }
        Machine machine = machineDAO.findByCode(code);        
        
        return MachineDTOConverter.toDTO(machine);
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findByBrand(String brand) {
        if (brand == null) {
            throw new IllegalArgumentException("Given brand cannot be null.");
        }
        List<Machine> machines = machineDAO.findByBrand(brand);
        
        List<MachineDTO> result = new ArrayList<MachineDTO>();
        for (Machine r : machines) {
            result.add(MachineDTOConverter.toDTO(r));
        }
        return result;
    }

    @Transactional(readOnly = true)
    public List<MachineDTO> findByYear(int year) {
        List<Machine> machines = machineDAO.findByYear(year);
        
        List<MachineDTO> result = new ArrayList<MachineDTO>();
        for (Machine r : machines) {
            result.add(MachineDTOConverter.toDTO(r));
        }
        return result;
    }
    
}
